package com.JdkProxy.secondexample;

import com.JdkProxy.firstexample.person.Person;
import com.JdkProxy.secondexample.pojo.PersonMajor;
import com.JdkProxy.secondexample.pojo.StuInvocationHandler;
import com.JdkProxy.secondexample.pojo.XiaoMingStudent;
import com.JdkProxy.secondexample.utils.WriteProxyFile;

import java.lang.reflect.Proxy;

/**
 * @User c.c
 * @Since 2022/11/25
 * @Time 9:25
 * @Description
 */

public class ProxyTest2 {
  public static void main(String[] args) {

    //创建一个实例对象，这个对象是被代理的对象
    PersonMajor student = new XiaoMingStudent("小米");

    //创建一个与代理对象相关联的InvocationHandler
    StuInvocationHandler<PersonMajor> stuInvocationHandler = new StuInvocationHandler<PersonMajor>(student);

    //创建一个代理对象PersonProxy来代理PersonMajor，代理对象的每个执行方法都会替换执行Invocation中的invoke方法
    PersonMajor PersonProxy = (PersonMajor) Proxy.newProxyInstance(StuInvocationHandler.class.getClassLoader(), new Class<?>[]{PersonMajor.class}, stuInvocationHandler);


    //代理执行上交班费的方法
    PersonProxy.giveMoney();

    WriteProxyFile writeProxyFile = new WriteProxyFile();

    writeProxyFile.writeFile();
  }
}
